<?php

/**
 * ResetPassword form.
 *
 * @package    docplus.co.za
 * @subpackage form
 * @author     Fox Matuku
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ResetPasswordForm extends sfForm {
	private $requiredFields = array('email');

	public function configure() {
		$this->widgetSchema['email'] = new sfWidgetFormInputText(array(), array('title' => 'Registered email', 'autocomplete' => 'off'));
        $this->widgetSchema['email']->setAttribute('class', 'prompt-text');

		$this->validatorSchema['email'] = new sfValidatorString(array('required' => true), array('required' => 'Your *registered email* is required.', 'invalid' => 'Your *registered email* is invalid.'));
		$this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, '_emailExistsValidator'))));

		$this->widgetSchema['email']->setLabel('Registered email');
		$this->widgetSchema->setNameFormat('reset_password[%s]');
	}

	public function _emailExistsValidator($validator, $values, $arguments) {
		foreach ($this->requiredFields as $field) {
			try {
				$this->validatorSchema[$field]->clean($values[$field]);
			}
			catch (sfValidatorErrorSchema $e) {
				return $values;
			}
			catch (sfValidatorError $e) {
				return $values;
			}
		}

		// try to fetch the registered user
		$gfUser = GfUserTable::getInstance()->findOneByEmail($values['email']);

		// check for both the existence and activation status of the user
		if (is_object($gfUser)) {
			return $values;
		}
		else {
			// seems the email has since disappeared off our database
			$this->validatorSchema['email']->setMessage('invalid', 'Your *registered email* was not found.');
		}

		// since we've reached here seems the supplied email is pretty invalid
		throw new sfValidatorErrorSchema($this->validatorSchema['email'], array('email' => new sfValidatorError($this->validatorSchema['email'], 'invalid')));
		return $values;
	}

}
